package com.wanganui.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * @author xtwang
 * @des 基础功能实现
 * @date 2025/2/12 下午3:52
 */
@RestController
@RequestMapping("/test/chat")
@Tag(name = "测试AI聊天")
@RequiredArgsConstructor
public class TestController {

    private final OllamaChatModel ollamaChatModel;

    @Operation(summary = "普通聊天")
    @GetMapping("/ai/generate")
    public Map<String, String> generate(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
        return Map.of("generation", this.ollamaChatModel.call(message));
    }

    @Operation(summary = "流式回答聊天")
    @GetMapping(value = "/ai/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
        return ollamaChatModel.stream(new Prompt(new UserMessage(message))).map(chatResponse -> chatResponse.getResult().getOutput().getText());
    }
}
